/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: com_message.h
 *
 * Purpose: message Base API define and implementation
 *
 * Developer:
 *   wen.gu , 2023-08-11
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_CORE_COM_MESSAGE_H__
#define __XBUS_CORE_COM_MESSAGE_H__

#include "xbus/core/xmessage.h"


namespace xbus::protocol {


class ComMessage {
public:
    using XComId = xbus::core::XComId;
    using XBufferPtr = core::XBufferPtr;
    using ComMessagePtr = std::shared_ptr<ComMessage>;
public:
    ComMessage(uint8_t type, uint32_t  capacity); 
    virtual ~ComMessage();
public:
    
    
public: /** for base Header */
    virtual uint8_t* data();
    virtual uint32_t length();

    virtual uint8_t type();
    virtual void set_type(uint8_t type);

    virtual uint16_t message_id();
    virtual void set_message_id(uint16_t id);
    virtual uint16_t session_id();
    virtual void set_session_id(uint16_t id);

    virtual uint8_t interface_version();
    virtual void set_interface_version(uint8_t);


    virtual uint8_t* payload();
    virtual uint32_t payload_size();

public:
    virtual core::XMessagePtr msg_ptr();
    virtual bool assign(uint32_t payload_size); /** assign a payload size */
    /** just fill data to payload segment from start_pos position, 
     * if (start_pos + size) > payload size, then the message buffer will resize to bigger than start_pos + size 
      */
    virtual bool fill_payload(const uint8_t* data, uint32_t size, uint32_t start_pos = 0); 

    virtual bool append_payload(const uint8_t* data, uint32_t size); /** will be add the length of payload and total message */

public: /** extension API, just as helper API, not as the data which to be sent */
    /** Indicate where the message com from, just used for receiver */
    virtual XComId src_com_id() const ;
    virtual void set_src_com_id(XComId fd); //this will be set by 'server' API

protected:
    core::XMessagePtr msg_ptr_ = nullptr;
};


using ComMessagePtr = ComMessage::ComMessagePtr;


} //namespace xbus::protocol


#endif /** !__XBUS_CORE_COM_MESSAGE_H__ */